﻿namespace Minesweeper
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    public static class HighScores
    {
        public static void NewScore(int score, string name)
        {
            string filename = @"..\..\TextFiles\scores.txt";
            int finalMines = LevelData.finalMines;
            int finalScore = 100 * finalMines - score;
            List<string> scoreList;
            if (File.Exists(filename))
                scoreList = File.ReadAllLines(filename).ToList();
            else

                scoreList = new List<string>();

            if (!string.IsNullOrWhiteSpace(name))
            {
                scoreList.Add(name + " " + finalScore.ToString());
            }
            var sortedScoreList = scoreList.OrderByDescending(ss => int.Parse(ss.Substring(ss.LastIndexOf(" ") + 1)));
            File.WriteAllLines(filename, sortedScoreList.ToArray());
        }

        public static void PrintHighScore()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            string filename = @"..\..\TextFiles\scores.txt";
            string spaces = new string(' ', 30);
            int xx = 1;
            List<string> scoreList = File.ReadAllLines(filename).ToList();
            string[] highScore =  
                {
                    @"            _    _  _         _        _____                        ",
                    @"           | |  | |(_)       | |      / ____|                       ",
                    @"           | |__| | _   __ _ | |__   | (___    ___  ___   _ __  ___ ",
                    @"           |  __  || | / _` || '_ \   \___ \  / __|/ _ \ | '__|/ _ \",
                    @"           | |  | || || (_| || | | |  ____) || (__  (_) || |     __/",
                    @"           |_|  |_||_| \__, ||_| |_| |_____/  \___|\___/ |_|   \___|",
                    @"                        __/ |                                       ",
                    @"                       |___/                                        "
                };
            foreach (string line in highScore)
            {
                Console.WriteLine(line);
            }
           
            Console.WriteLine();
            
       
            Console.ForegroundColor = ConsoleColor.Cyan;
            for (int i = 0; i < 15; i++)
            {
                if (i >= scoreList.Count)
                {
                    break;
                }
                Console.WriteLine("{0}{1}", spaces, xx.ToString() + ". " + scoreList[i]);
                xx++;
            }
            
            Console.ReadKey();
            MinesweeperGame.ResetGameStatusAndContinue();            
        }
    }
}
